package com.hzhk.happyrent.ui.adapter.viewpager;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.hzhk.happyrent.utils.ListUtils;
import com.hzhk.happyrent.utils.glide.ShowImageUtils;

import java.util.List;

/**
 * User: hzhk(1097282555@qq.com)
 * Date: 2017/11/27
 * Time: 09:25
 * progject: hzk
 * Describe： ${Describe}
 */
public class PicAdapter extends PagerAdapter {
    private Context context;
    private List<String> url;
    public PicAdapter(Context context){
        this.context=context;
    }
    public void setData(List<String> url){
        this.url=url;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return ListUtils.isEmpty(url)?0:url.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }
    public Object instantiateItem(final ViewGroup container, int position) {

        position = position % url.size();
        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        ShowImageUtils.showImageViewToCircleNoPlaceHolder(context,url.get(position),imageView,10);
        ViewParent parent = imageView.getParent();
        if (parent != null) {
            ViewGroup viewGroup = (ViewGroup) parent;
            viewGroup.removeView(imageView);
        }
        imageView.setTag(position);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        object = null;
    }
}
